<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    let obj = new Proxy({}, {
      set(target, prop, value) {
        // 设置和拦截
        if (prop == "age") {
          if (!Number.isInteger(value)) {
            throw new TypeError(`年龄必须为整数`);
          };
          if (value > 200) {
            throw new RangeError(`年龄必须小于200岁`);
          };
        };
        target[prop] = value;
      }
    });
    obj.a = 12345;
    obj.name = "好人好是";
    console.log(obj);
  </script>
  <script>
    let json = {
      a: "大家好",
      b: 18,
      c: "吃饭了么"
    };
    let newJson = new Proxy(json, {
      deleteProperty(target, property) {
        // 删除某值时做的处理
        console.log(`您是否要删除${property}属性`);
        delete target[property];
      },
      has(target, property) {
        // 判断某值是否存在
        console.log(`判断是否存在调用has方法`);
        return property in target;
      }
    });
    console.log("a" in newJson);
    delete newJson.a;
  </script>
  <script>
    let fn = () => {
      console.log(`我是函数`);
    };
    let newFn = new Proxy(fn, {
      apply() {
        // 拦截
        return "是函数么?"
      }
    });
  </script>
  <script>
    /* Reflect.apply(调用的函数,this指向,参数数组) */
    let res = Reflect.apply(Math.ceil, null, [9.8]);
    console.log(res);
    let sum = (a, b) => {
      return a + b;
    }
    let newSum = new Proxy(sum, {
      apply(target, content, args) {
        console.log(target);
        console.log(content);
        console.log(args);
        return Reflect.apply(...arguments);
      }
    });
    console.log(newSum(2, 3));
  </script>
  <script>
    "use strict";
    var s = Symbol('foo');
    var k = 'bar';
    var o = {
      [s]: 1,
      [k]: 1
    };
    let ii = Reflect.ownKeys(o) //与Object.keys();
    console.log(ii);
  </script>
</body>

</html>